<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.util.*,org.apache.commons.fileupload.*,com.news.util.*,java.io.*,org.apache.commons.fileupload.disk.DiskFileItemFactory,org.apache.commons.fileupload.servlet.ServletFileUpload;" %>
<%@page import="java.sql.*;" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <%
            String pic = request.getParameter("pic");
            String type = request.getParameter("type");
            String id = request.getParameter("id");
            final String updateLinkSQL = "update link set img_logo=? where id=?";
            final String updateNewsSQL = "update news set pic=?,picfile=? where id=?";
            String SQL = null;
            String Folder = null;
            if (type != null && !type.equalsIgnoreCase("")) {
                if (type.equalsIgnoreCase("link")) {
                    SQL = updateLinkSQL;
                    Folder = NewsConfig.logoFolder;
                }
                if (type.equalsIgnoreCase("news")) {
                    SQL = updateNewsSQL;
                    Folder = NewsConfig.newPicFolder;
                }
            }

            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            try {
                if (isMultipart) {
                    String path = this.getServletContext().getRealPath("/");
                    path += Folder;
                    // Create a factory for disk-based file items
                    FileItemFactory factory = new DiskFileItemFactory();
                    // Create a new file upload handler
                    ServletFileUpload upload = new ServletFileUpload(factory);
                    upload.setHeaderEncoding("UTF-8");

                    // Parse the request
                    List items = upload.parseRequest(request); /* FileItem */
                    /* FileItem */
                    Iterator iter = items.iterator();
                    while (iter.hasNext()) {
                        FileItem item = (FileItem) iter.next();
                        if (!item.isFormField()) {
                            String file = item.getName();
                            String fileType = null;
                            if (file.lastIndexOf(".") > 0) {
                                fileType = file.substring(file.lastIndexOf("."));
                            }
                            String uuid = UUID.randomUUID().toString();
                            file = uuid + fileType;
                            path += file;
                            File picFile = new File(path);
                            if (!picFile.getParentFile().exists()) {
                                picFile.getParentFile().mkdirs();
                            }

                            FileOutputStream fos = new FileOutputStream(picFile);
                            byte[] b = item.get();
                            fos.write(b);
                            fos.flush();
                            fos.close();

                            String picPath = Folder + file;
                            PreparedStatement ps = DButil.getPreparedStatement(SQL);

                            if (type != null && !type.equalsIgnoreCase("")) {
                                if (type.equalsIgnoreCase("link")) {
                                    ps.setString(1, picPath);
                                    ps.setInt(2, Integer.valueOf(id));
                                }
                                if (type.equalsIgnoreCase("news")) {
                                    ps.setString(1, picPath);
                                    ps.setBytes(2, b);
                                    ps.setInt(3, Integer.valueOf(id));
                                }
                            }
                            ps.executeUpdate();
                            pic = picPath;
                            break;
                        }
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                DButil.close();
            }
        %>

        <style type="text/css">
            * {margin:0;padding:0}
            div {
                width:350px;
                height:200px;
                overflow:hidden;
                position:relative;
                display:table-cell;
                text-align:center;
                vertical-align:middle
            }
            div p {
                position:static;
                +position:absolute;
                top:50%
            }
            img {
                position:static;
                +position:relative;
                top:-50%;left:-50%;
            }
        </style>

    </head>
    <body>
        <div><p><img align="middle" src="<%=pic%>" onerror="this.src='img/nopic.jpg'" onload="if(this.height>200){this.height=200}" /></p></div>
    </body>
</html>
